package com.tct.worktimeboard.feishu;

import com.alibaba.fastjson.JSONObject;
import com.tct.worktimeboard.config.DataSourceContextHolder;
import com.tct.worktimeboard.entity.UserInfo;
import com.tct.worktimeboard.entity.Work;
import com.tct.worktimeboard.mapper.UserInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Huhailong
 * @Description 同构飞书给指定人员发送消息
 * @Date 2021/5/10.
 */
@Slf4j
@Component
public class SendMessage {

    @Value("${feishu.api-url}")
    private String appUrl;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    FeiShuUtil feiShuUtil;
    @Resource
    UserInfoMapper userInfoMapper;

    /**
     * 指定单个用户返送
     * @param entity 消息体
     */
    public void sendMessageToOne(MessageEntity entity){
        String requestUrl = appUrl+"im/v1/messages?receive_id_type=open_id";    //指定receive_id_type为open_id
        String tenantAccessToken = feiShuUtil.getTenantAccessToken();
        HttpHeaders headers = feiShuUtil.makeHeaders(tenantAccessToken);
        HttpEntity<String> request = new HttpEntity<>(JSONObject.toJSONString(entity),headers);
        restTemplate.postForEntity(requestUrl,request,String.class);
    }

    /**
     * 批量发送消息
     * @param entity 消息体
     */
    public void sendMessageBatch(MessageEntity entity){
        String requestUrl = appUrl+"message/v4/batch_send/";
        String tenantAccessToken = feiShuUtil.getTenantAccessToken();
        HttpHeaders headers = feiShuUtil.makeHeaders(tenantAccessToken);
        HttpEntity<String> request = new HttpEntity<>(JSONObject.toJSONString(entity),headers);
        restTemplate.postForEntity(requestUrl, request, String.class);
        int size = entity.getOpen_ids().size();
        log.info("已发送消息数：{}个人",size);
    }

    @Async  //异步执行
    public void sendMakeWorkTimeMessage(Work work){
        String makeContent = "{\"zh_cn\":{\"title\":\"工时审批提醒\",\"content\":[[{\"tag\":\"text\",\"text\":\"胡海龙填报了工时等待您的审批。\"},{\"tag\":\"a\",\"href\":\"http://172.52.0.3:8085/webpage/work-time-board\",\"text\":\"内网地址 | \"},{\"tag\":\"a\",\"href\":\"http://114.242.175.67:8085/webpage/work-time-board/out\",\"text\":\"外网地址\"}],[{\"tag\":\"text\",\"text\":\"工作描述："+work.getWorkDescribe()+"\"}]]}}";
        String noPassContent = "{\"zh_cn\":{\"title\":\"工时审批未通过提醒\",\"content\":[[{\"tag\":\"text\",\"text\":\"您的工时审批未通过\"},{\"tag\":\"a\",\"href\":\"http://172.52.0.3:8085/webpage/work-time-board\",\"text\":\"内网地址 | \"},{\"tag\":\"a\",\"href\":\"http://114.242.175.67:8085/webpage/work-time-board/out\",\"text\":\"外网地址\"}],[{\"tag\":\"text\",\"text\":\"未通过原因："+work.getEvaluate()+"\"}]]}}";
        String passContent = "{\"zh_cn\":{\"title\":\"工时审通过提醒\",\"content\":[[{\"tag\":\"text\",\"text\":\"您的工时审批已通过\"},{\"tag\":\"a\",\"href\":\"http://172.52.0.3:8085/webpage/work-time-board\",\"text\":\"内网地址 | \"},{\"tag\":\"a\",\"href\":\"http://114.242.175.67:8085/webpage/work-time-board/out\",\"text\":\"外网地址\"}],[{\"tag\":\"text\",\"text\":\"工时描述："+work.getWorkDescribe()+"\"}]]}}";
        DataSourceContextHolder.setContextHolder("db2");
        MessageEntity entity = new MessageEntity();
        String phone;
        if(work.getMakeStatus()==null){
            phone = userInfoMapper.selectById(work.getMakeUserId()).getPhone();  //获取到审批人手机号
            entity.setContent(makeContent);
        }else {
            phone = userInfoMapper.selectById(work.getUserId()).getPhone(); //获取填报人手机号
            if (work.getMakeStatus()) {
                entity.setContent(passContent);
            } else {
                entity.setContent(noPassContent);
            }
        }
        DataSourceContextHolder.clear();
        String openId = feiShuUtil.getUserInfo(phone).get("openId");
        entity.setMsg_type("post");
        entity.setReceive_id(openId);
        sendMessageToOne(entity);
    }

}
